﻿
''' <summary>
''' 代理行信息查詢
''' </summary>
''' <remarks>
''' Create Date:2008-04-23
''' Create Person:huyang
''' </remarks>
Public Class Frm_CSAgentInformation

    Implements IDServer.IDServer.IDServer

#Region "Variabled"

#Region "System Variabled Define"


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Private WithEvents MainForm As Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property


#End Region

#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

    Public Fields As String

    Public FindBool As Boolean = True


#End Region

#End Region

#Region "Methods"

#Region "initialization"

    Private Sub Initialize()
        Me.AddHandlers(New Control() {Me.Panel1, Me.GroupBox2})
        Dim Ds As DataSet = Me.ReturnInitDs()
        Me.FillCombox(Ds.Tables(0))
        Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
        cs.SetControlsEnabled(False)
        Dim imeMode As clsIme = New clsIme()
        imeMode.AddHandler_ImeMode(New Control() {Panel1, GroupBox2})
    End Sub

    Private Sub FillCombox(ByVal dtab As DataTable)

        Me.cbx_fTypeCode.DataSource = dtab
        Me.cbx_fTypeCode.DisplayMember = "fTypeName"
        Me.cbx_fTypeCode.ValueMember = "fTypeNo"
        Me.cbx_fTypeCode.Text = ""
        Me.cbx_fTypeCode.SelectedIndex = -1

        Me.cbxfTypeName.DataSource = dtab.Copy()
        Me.cbxfTypeName.DisplayMember = "fTypeName"
        Me.cbxfTypeName.ValueMember = "fTypeNo"
        Me.cbxfTypeName.Text = ""
        Me.cbxfTypeName.SelectedIndex = -1
 
    End Sub

    Private Function ReturnInitDs() As DataSet
        Dim ds As New DataSet

        Dim sql As String = ""
        sql &= " select * from CS_AgentType " & vbCrLf
        Try
            ds = Exec(sql, E_ExecType.DataSet)
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return ds
    End Function

#End Region

#Region "ExecFind"

    Private Function ReturnWhereExp()
        Dim sql As String = ""

        If Me.fLoadBool = False Then
            sql &= "   where 1<>1 "
        Else
            sql &= "  where 1=1 "

            If Me.txt_ShortCode.Text.Trim <> "" Then
                sql &= "  and ShortCode like '%" & Replace_Text(Me.txt_ShortCode.Text.Trim) & "%'"
            End If
            If Me.txt_AgentName.Text.Trim <> "" Then
                sql &= "  and AgentName like '%" & Me.txt_AgentName.Text.Trim & "%'"
            End If
            If Me.txt_AgentBrand.Text.Trim <> "" Then
                sql &= "  and AgentBrand like '%" & Replace_Text(Me.txt_AgentBrand.Text.Trim) & "%'"
            End If
            If Me.cbx_fTypeCode.Text.Trim <> "" AndAlso Not Me.cbx_fTypeCode.SelectedValue Is Nothing Then
                sql &= "  and fTypeCode='" & Me.cbx_fTypeCode.SelectedValue.ToString() & "'"
            End If

            If Me.txt_QuickSearch.Text.Trim <> "" Then

                sql &= "  and (ShortCode like '%" & Me.txt_QuickSearch.Text.Trim & "%'   or  AgentName like '%" & Me.txt_QuickSearch.Text & "%'   or AgentBrand like '%" & Me.txt_QuickSearch.Text & "%'   or  Address like '%" & Me.txt_QuickSearch.Text.Trim & "%'  or a.Remark like '%" & Me.txt_QuickSearch.Text & "%'   or AgentEnname like '%" & Me.txt_QuickSearch.Text.Trim & "%')"

            End If

        End If

        Return sql
    End Function

    Private Sub ExecFind()
        Me.Cursor = Cursors.WaitCursor
        Dim whereExp As String = Me.ReturnWhereExp()

        Dim sql As String = ""

        sql &= " select a.Id,AgentCode,ShortCode,AgentName,AgentEnname,fTypeCode,b.fTypeName," & vbCrLf
        sql &= " AgentBrand,ContactPsn,Telphone,Fax,Cellphone,ContactPsn2,Telphone2,Fax2," & vbCrLf
        sql &= " Cellphone2,Consultant,ConsultantPhone,Consultant2,ConsultantPhone2," & vbCrLf
        sql &= " Email, Address, a.Remark, DeliverRemark, MaintainRemark, OtherRmark" & vbCrLf
        sql &= " from CS_AgentInformation     a left join CS_AgentType b on a.fTypeCode=b.fTypeNo    " & whereExp & vbCrLf

        Dim Dtab As DataTable = New DataTable()

        Try
            Dtab = Exec(sql, E_ExecType.DataTable)
            Me.DGV_Master.DataSource = Dtab

            BindDataGridViewCaption(Me.DGV_Master, Me.ReturnCaption(1))
            FullFormatDataGridView(Me.DGV_Master, False, False)
            Me.DGV_Master.SelectionMode = DataGridViewSelectionMode.CellSelect

            DGV_Master.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
            Dim isFind As Boolean = Me.DGV_Master.RowCount > 0

            If Me.fLoadBool = True AndAlso FindBool = True Then
                Me.SetButton(ButtonType.Browse, isFind)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    Private Function ReturnCaption(ByVal Index As Integer) As Hashtable
        Dim Hash As New Hashtable

        Select Case Index
            Case 1
                Hash.Add("AgentCode", "編號")
                Hash.Add("ShortCode", "代號")
                Hash.Add("AgentName", "代理行名稱")
                Hash.Add("AgentEnname", "英文名稱")
                Hash.Add("fTypeName", "類型")
                Hash.Add("AgentBrand", "代理品牌")
                Hash.Add("ContactPsn", "聯系人")
                Hash.Add("Telphone", "聯系電話")
                Hash.Add("Fax", "傳真")
                Hash.Add("Cellphone", "移動電話")
                Hash.Add("ContactPsn2", "聯系人2")
                Hash.Add("Telphone2", "聯系電話2")
                Hash.Add("Fax2", "傳真2")
                Hash.Add("Cellphone2", "移動電話2")
                Hash.Add("Consultant", "營業顧問")
                Hash.Add("ConsultantPhone", "顧問電話")
                Hash.Add("Consultant2", "營業顧問2")
                Hash.Add("ConsultantPhone2", "顧問電話2")
                Hash.Add("Email", "Email")
                Hash.Add("Address", "地址")
                Hash.Add("Remark", "備注")
                Hash.Add("DeliverRemark", "送貨備注")
                Hash.Add("MaintainRemark", "維修備注")
                Hash.Add("OtherRmark", "其它")
        End Select

        Return Hash
    End Function

#End Region

#Region "setEnabled"

    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type">操作類型</param>
    ''' <param name="IsFind">是否是流覽</param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try

            Select Case type
                Case ButtonType.Add
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Browse
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True
                        .ToolStripCheck.Enabled = False
                        .ToolStripComplete.Enabled = False
                        .ToolStripUnCheck.Enabled = False
                    End With

                Case ButtonType.Cancel
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

#End Region

#Region "FillDetail"

    Private Sub FillDetail(ByVal AgentCode As String)
        Try
            Dim Dtab As DataTable = DirectCast(Me.DGV_Master.DataSource, DataTable)
            Dim DRow() As DataRow = Dtab.Select("AgentCode='" & AgentCode & "'")

            If DRow.Length > 0 Then
                Me.txtAgentCode.Text = DRow(0).Item("AgentCode") & ""
                Me.txtShortCode.Text = DRow(0).Item("ShortCode") & ""
                Me.txtAgentName.Text = DRow(0).Item("AgentName") & ""
                Me.txtAgentEnname.Text = DRow(0).Item("AgentEnname") & ""
                Me.cbxfTypeName.Text = DRow(0).Item("fTypeName") & ""
                Me.txtAgentBrand.Text = DRow(0).Item("AgentBrand") & ""

                Me.txtContactPsn.Text = DRow(0).Item("ContactPsn") & ""
                Me.txtTelphone.Text = DRow(0).Item("Telphone") & ""
                Me.txtFax.Text = DRow(0).Item("Fax") & ""
                Me.txtCellphone.Text = DRow(0).Item("Cellphone") & ""
                Me.txtContactPsn2.Text = DRow(0).Item("ContactPsn2") & ""
                Me.txtTelphone2.Text = DRow(0).Item("Telphone2") & ""
                Me.txtFax2.Text = DRow(0).Item("Fax2") & ""
                Me.txtCellphone2.Text = DRow(0).Item("Cellphone2") & ""
                Me.txtConsultant.Text = DRow(0).Item("Consultant") & ""
                Me.txtConsultantPhone.Text = DRow(0).Item("ConsultantPhone") & ""
                Me.txtConsultant2.Text = DRow(0).Item("Consultant2") & ""
                Me.txtConsultantPhone2.Text = DRow(0).Item("ConsultantPhone2") & ""
                Me.txtEmail.Text = DRow(0).Item("Email") & ""
                Me.txtAddress.Text = DRow(0).Item("Address") & ""
                Me.RichTextBoxRemark.Text = DRow(0).Item("Remark") & ""
                Me.txtMaintainRemark.Text = DRow(0).Item("MaintainRemark") & ""
                Me.txtDeliverRemark.Text = DRow(0).Item("DeliverRemark") & ""
                Me.txtOtherRmark.Text = DRow(0).Item("OtherRmark") & ""
            Else
                Me.txtAgentCode.Text = ""
                Me.txtShortCode.Text = ""
                Me.txtAgentName.Text = ""
                Me.txtAgentEnname.Text = ""
                Me.cbxfTypeName.Text = ""
                Me.txtAgentBrand.Text = ""
                Me.txtContactPsn.Text = ""
                Me.txtTelphone.Text = ""
                Me.txtFax.Text = ""
                Me.txtCellphone.Text = ""
                Me.txtContactPsn2.Text = ""
                Me.txtTelphone2.Text = ""
                Me.txtFax2.Text = ""
                Me.txtCellphone2.Text = ""
                Me.txtConsultant.Text = ""
                Me.txtConsultantPhone.Text = ""
                Me.txtConsultant2.Text = ""
                Me.txtConsultantPhone2.Text = ""
                Me.txtEmail.Text = ""
                Me.txtAddress.Text = ""
                Me.RichTextBoxRemark.Text = ""
                Me.txtMaintainRemark.Text = ""
                Me.txtDeliverRemark.Text = ""
                Me.txtOtherRmark.Text = ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region


#Region "AddHandler Events"

    ''' <summary>
    ''' KeyDown Event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            Select Case e.KeyCode
                Case Keys.Enter
                    If e.Shift = True Then
                        If TypeOf sender Is ComboBox Then
                            If CType(sender, ComboBox).DroppedDown = True Then
                                CType(sender, ComboBox).DroppedDown = False
                            End If
                        End If
                        Me.ProcessTabKey(False)
                        SendKeys.Send("{F4}")
                    Else
                        If TypeOf sender Is ComboBox Then
                            If CType(sender, ComboBox).DroppedDown = True Then
                                CType(sender, ComboBox).DroppedDown = False
                            End If
                        End If

                        Me.ProcessTabKey(True)
                        SendKeys.Send("{F4}")

                    End If
            End Select
        Catch ex As Exception
            MsgBox("TextBox_KeyDown:" & ex.Message & ex.StackTrace, MsgBoxStyle.Critical And MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 添加自定義事件
    ''' </summary>
    ''' <param name="Pans"></param>
    ''' <remarks></remarks>
    Private Sub AddHandlers(ByVal Pans() As Control)
        Try
            For Each Pan As Control In Pans
                For Each Ctrl As Control In Pan.Controls
                    If TypeOf Ctrl Is TextBox _
                       OrElse TypeOf Ctrl Is ComboBox _
                       OrElse TypeOf Ctrl Is DateTimePicker Then
                        AddHandler Ctrl.KeyDown, AddressOf TextBox_KeyDown
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox("AddHandlers:" & ex.Message & ex.StackTrace, MsgBoxStyle.Critical And MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

#End Region

#End Region

#Region "Events"

#Region "FormLoad"

    Private Sub Frm_CSAgentInformation_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.fLoadBool = False
        Me.Initialize()
        addColtrolEvents()
        Me.ExecFind()
        SetGridCaptionVisible(Me.DGV_Master, Fields, "Frm_CSAgentInformation")
        Me.fLoadBool = True
    End Sub

#End Region

#Region "Button"

    Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Me.ExecFind()
    End Sub

#End Region

#Region "DataGridView"

    Private Sub DGV_Master_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_Master.CellDoubleClick
        Me.TabControl1.SelectedIndex = 1
    End Sub


    Private Sub DGV_Master_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGV_Master.RowPostPaint
        Try
            Dim Grid As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(Grid.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
            SBrush = Nothing
        Catch ex As Exception
            MessageBox.Show("DGV_Master_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "TabControl"

    Private Sub TabControl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TabControl1.KeyDown
        Select Case e.KeyCode
            Case Keys.S
                If e.Control = True Then
                    If (ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd OrElse ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit) Then
                        MainForm.ToolStripSave.PerformClick()
                    End If
                End If
        End Select
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        If Me.TabControl1.SelectedIndex = 1 Then
            Dim AgentCode As String = ""
            If Not Me.DGV_Master.CurrentRow Is Nothing Then
                AgentCode = Me.DGV_Master.CurrentRow.Cells("AgentCode").Value & ""
            End If
            FillDetail(AgentCode)
        End If
    End Sub

    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd OrElse ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
            e.Cancel = True
        End If
    End Sub



#End Region

#Region "SaveData"

    Private Function Validated1() As Boolean
        If Me.txtAgentName.Text.Trim = "" Then
            MessageBox.Show("代理行名稱不能為空。", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Me.txtAgentName.Focus()
            Return False
        End If

        Return True
    End Function

    Private Function SaveData() As Boolean
        Dim bSuccess As Boolean = False

        Dim AgentCode As String = Me.txtAgentCode.Text.Trim
        Dim ShortCode As String = Me.txtShortCode.Text.Trim
        Dim AgentName As String = Me.txtAgentName.Text.Trim
        Dim AgentEnname As String = Me.txtAgentEnname.Text.Trim
        Dim fTypeCode As String = Me.cbxfTypeName.SelectedValue & ""
        Dim fTypeName As String = Me.cbxfTypeName.Text.Trim
        Dim AgentBrand As String = Me.txtAgentBrand.Text.Trim
        Dim ContactPsn As String = Me.txtContactPsn.Text.Trim
        Dim Telphone As String = Me.txtTelphone.Text.Trim
        Dim Fax As String = Me.txtFax.Text.Trim
        Dim Cellphone As String = Me.txtCellphone.Text.Trim

        Dim ContactPsn2 As String = Me.txtContactPsn2.Text.Trim
        Dim Telphone2 As String = Me.txtTelphone2.Text.Trim
        Dim Fax2 As String = Me.txtFax2.Text.Trim
        Dim Cellphone2 As String = Me.txtCellphone2.Text.Trim

        Dim Consultant As String = Me.txtConsultant.Text.Trim
        Dim ConsultantPhone As String = Me.txtConsultantPhone.Text.Trim
        Dim Consultant2 As String = Me.txtConsultant2.Text.Trim
        Dim ConsultantPhone2 As String = Me.txtConsultantPhone2.Text.Trim
        Dim Email As String = Me.txtEmail.Text.Trim
        Dim Address As String = Me.txtAddress.Text.Trim
        Dim Remark As String = Me.RichTextBoxRemark.Text.Trim
        Dim DeliverRemark As String = Me.txtDeliverRemark.Text.Trim
        Dim MaintainRemark As String = Me.txtMaintainRemark.Text.Trim
        Dim OtherRmark As String = Me.txtOtherRmark.Text.Trim

        Dim sql As System.Text.StringBuilder = New System.Text.StringBuilder()

        Try

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                AgentCode = Exec("select Isnull(Max(AgentCode),0) from CS_AgentInformation", E_ExecType.ExecuteScalar) & ""
                AgentCode = (Val(AgentCode) + 1).ToString("00000")
                Me.txtAgentCode.Text = AgentCode
                sql.Append("Insert Into CS_AgentInformation(" & vbCrLf)
                sql.Append("AgentCode,ShortCode,AgentName,AgentEnname,fTypeCode,fTypeName,AgentBrand,")
                sql.Append("ContactPsn,Telphone,Fax,Cellphone,ContactPsn2,Telphone2,Fax2,")
                sql.Append("Cellphone2,Consultant,ConsultantPhone,Consultant2,ConsultantPhone2,")
                sql.Append("Email,Address,Remark,DeliverRemark,MaintainRemark,OtherRmark)")
                sql.Append("values(" & vbCrLf)
                sql.Append("'" & AgentCode & "','" & ShortCode & "','" & AgentName & "','" & AgentEnname & "','" & fTypeCode & "',Null,'" & AgentBrand & "',")
                sql.Append("'" & ContactPsn & "','" & Telphone & "','" & Fax & "','" & Cellphone & "','" & ContactPsn2 & "','" & Telphone2 & "','" & Fax2 & "',")
                sql.Append("'" & Cellphone2 & "','" & Consultant & "','" & ConsultantPhone & "','" & Consultant2 & "','" & ConsultantPhone2 & "',")
                sql.Append("'" & Email & "','" & Address & "','" & Remark & "','" & DeliverRemark & "','" & MaintainRemark & "','" & OtherRmark & "'")
                sql.Append(")" & vbCrLf)
            Else
                sql.Append("UPDATE [Test].[dbo].[CS_AgentInformation]")
                sql.Append("    SET ")
                sql.Append("            [ShortCode] = '" & ShortCode & "'")
                sql.Append("           ,[AgentName] = '" & AgentName & "'")
                sql.Append("           ,[AgentEnname] = '" & AgentEnname & "'")
                sql.Append("           ,[fTypeCode] = '" & fTypeCode & "'")
                sql.Append("           ,[fTypeName] = null")
                sql.Append("           ,[AgentBrand] = '" & AgentBrand & "'")
                sql.Append("           ,[ContactPsn] = '" & ContactPsn & "'")
                sql.Append("           ,[Telphone] = '" & Telphone & "'")
                sql.Append("           ,[Fax] = '" & Fax & "'")
                sql.Append("           ,[ContactPsn2] = '" & ContactPsn2 & "'")
                sql.Append("           ,[Telphone2] = '" & Telphone2 & "'")
                sql.Append("           ,[Fax2] = '" & Fax2 & "'")
                sql.Append("           ,[Cellphone] = '" & Cellphone & "'")
                sql.Append("           ,[Cellphone2] = '" & Cellphone2 & "'")
                sql.Append("           ,[Consultant] = '" & Consultant & "'")
                sql.Append("           ,[Consultant2] = '" & Consultant2 & "'")
                sql.Append("           ,[ConsultantPhone] = '" & ConsultantPhone & "'")
                sql.Append("           ,[ConsultantPhone2] = '" & ConsultantPhone2 & "'")
                sql.Append("           ,[Email] = '" & Email & "'")
                sql.Append("           ,[Address] = '" & Address & "'")
                sql.Append("           ,[Remark] = '" & Remark & "'")
                sql.Append("           ,[DeliverRemark] = '" & DeliverRemark & "'")
                sql.Append("           ,[MaintainRemark] = '" & MaintainRemark & "'")
                sql.Append("           ,[OtherRmark] = '" & OtherRmark & "'")
                sql.Append("           WHERE  AgentCode = '" & AgentCode & "'")
            End If

            Exec(sql.ToString(), E_ExecType.ExecuteNonQuery, True)
            UpdateData(AgentCode, ShortCode, AgentName, AgentEnname, fTypeCode, fTypeName, AgentBrand, _
                                ContactPsn, Telphone, Fax, Cellphone, ContactPsn2, Telphone2, Fax2, _
                                Cellphone2, Consultant, ConsultantPhone, Consultant2, ConsultantPhone2, _
                                Email, Address, Remark, DeliverRemark, MaintainRemark, OtherRmark)

            bSuccess = True

        Catch ex As Exception
            bSuccess = False
            MessageBox.Show(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.resources.ErrAdd, My.resources.ErrEdit), Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        If bSuccess = True Then
            MessageBox.Show(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.resources.Addsuccess, My.resources.EditSuccess), Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
        Me.SetButton(ButtonType.Browse, True)
        Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
        cs.SetControlsEnabled(False)
        Me.RichTextBoxRemark.BackColor = Me.txtAgentName.BackColor

        Return True
    End Function

    Private Function UpdateData(ByVal AgentCode As String, ByVal ShortCode As String, ByVal AgentName As String, ByVal AgentEnname As String, ByVal fTypeCode As String, ByVal fTypeName As String, ByVal AgentBrand As String, _
                                                  ByVal ContactPsn As String, ByVal Telphone As String, ByVal Fax As String, ByVal Cellphone As String, ByVal ContactPsn2 As String, ByVal Telphone2 As String, ByVal Fax2 As String, _
                                                  ByVal Cellphone2 As String, ByVal Consultant As String, ByVal ConsultantPhone As String, ByVal Consultant2 As String, ByVal ConsultantPhone2 As String, _
                                                  ByVal Email As String, ByVal Address As String, ByVal Remark As String, ByVal DeliverRemark As String, ByVal MaintainRemark As String, ByVal OtherRmark As String) As Boolean
        Dim bSuccess As Boolean
        Try
            Dim Dtab As DataTable = DirectCast(Me.DGV_Master.DataSource, DataTable)

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then

                Dim row As DataRow = Dtab.NewRow()
                row("AgentCode") = AgentCode
                row("ShortCode") = ShortCode
                row("AgentName") = AgentName
                row("AgentEnname") = AgentEnname
                row("fTypeCode") = fTypeCode
                row("fTypeName") = fTypeName
                row("AgentBrand") = AgentBrand
                row("ContactPsn") = ContactPsn
                row("Telphone") = Telphone
                row("Fax") = Fax
                row("Cellphone") = Cellphone
                row("ContactPsn2") = ContactPsn2
                row("Telphone2") = Telphone2
                row("Fax2") = Fax2
                row("Cellphone2") = Cellphone2
                row("Consultant") = Consultant
                row("ConsultantPhone") = ConsultantPhone
                row("Consultant2") = Consultant2
                row("ConsultantPhone2") = ConsultantPhone2
                row("Email") = Email
                row("Address") = Address
                row("Remark") = Remark
                row("DeliverRemark") = DeliverRemark
                row("MaintainRemark") = MaintainRemark
                row("OtherRmark") = OtherRmark

                Dtab.Rows.Add(row)
            Else
                Dim DRow() As DataRow = Dtab.Select("AgentCode='" & AgentCode & "'")
                If DRow.Length > 0 Then
                    Dim row As DataRow = DRow(0)
                    row("ShortCode") = ShortCode
                    row("AgentName") = AgentName
                    row("AgentEnname") = AgentEnname
                    row("fTypeCode") = fTypeCode
                    row("fTypeName") = fTypeName
                    row("AgentBrand") = AgentBrand
                    row("ContactPsn") = ContactPsn
                    row("Telphone") = Telphone
                    row("Fax") = Fax
                    row("Cellphone") = Cellphone
                    row("ContactPsn2") = ContactPsn2
                    row("Telphone2") = Telphone2
                    row("Fax2") = Fax2
                    row("Cellphone2") = Cellphone2
                    row("Consultant") = Consultant
                    row("ConsultantPhone") = ConsultantPhone
                    row("Consultant2") = Consultant2
                    row("ConsultantPhone2") = ConsultantPhone2
                    row("Email") = Email
                    row("Address") = Address
                    row("Remark") = Remark
                    row("DeliverRemark") = DeliverRemark
                    row("MaintainRemark") = MaintainRemark
                    row("OtherRmark") = OtherRmark
                End If
            End If
            Dtab.AcceptChanges()
            Me.DGV_Master.Update()
            Me.DGV_Master.Refresh()
            bSuccess = True
        Catch ex As Exception
            bSuccess = False
            MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return bSuccess
    End Function

#End Region

#End Region

#Region "Interface"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        Try
            If AddEnable = False Then
                MessageBox.Show("對不起，您沒有新增權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
            ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
            Me.FillDetail("")
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(True)
            Me.txtAgentCode.ReadOnly = True
            Me.SetButton(ButtonType.Add, False)
            Me.txtShortCode.Focus()
            Me.RichTextBoxRemark.ReadOnly = False
            Me.RichTextBoxRemark.BackColor = System.Drawing.SystemColors.ActiveCaptionText
            Me.RichTextBoxRemark.Refresh()
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            Dim AgentCode As String = ""
            Dim IsFind As Boolean = False
            If Not Me.DGV_Master.CurrentRow Is Nothing Then
                AgentCode = Me.DGV_Master.CurrentRow.Cells("AgentCode").Value & ""
                IsFind = True
            End If
            Me.FillDetail(AgentCode)
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(False)
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            Me.SetButton(ButtonType.Browse, IsFind)
            Me.RichTextBoxRemark.BackColor = Me.txtShortCode.BackColor
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Try
            If DeleteEnable = False Then
                MessageBox.Show("對不起，您沒有刪除權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.DGV_Master.CurrentRow Is Nothing Then Return False
            If MessageBox.Show(My.resources.ConfirmDelete, Msg.CompanyName, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) <> DialogResult.OK Then Return False
            Dim Dtab As DataTable = DirectCast(Me.DGV_Master.DataSource, DataTable)

            Dim AgentCode As String = Me.DGV_Master.CurrentRow.Cells("AgentCode").Value & ""

            Dim sql As String = "Delete CS_AgentInformation where AgentCode='" & AgentCode & "'  " & vbCrLf
            Exec(sql, E_ExecType.ExecuteNonQuery)

            Dim drow() As DataRow = Dtab.Select("AgentCode='" & AgentCode & "'")
            If drow.Length > 0 Then
                drow(0).Delete()
            End If
            Dtab.AcceptChanges()

            If Me.TabControl1.SelectedIndex = 0 Then
                Dim isFind As Boolean = False
                If Me.DGV_Master.RowCount > 0 Then
                    isFind = True
                End If
                Me.SetButton(ButtonType.Browse, isFind)
            Else
                Me.FillDetail("")
                Me.SetButton(ButtonType.Browse, False)
            End If
            MessageBox.Show(My.resources.DeleteSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return True
        Catch ex As Exception
            MessageBox.Show(My.resources.ErrDelete, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try
            If EditEnable = False Then
                MessageBox.Show("對不起，您沒有修改權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(True)
            Me.txtAgentCode.ReadOnly = True
            Me.SetButton(ButtonType.Edit, False)
            ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
            Me.RichTextBoxRemark.BackColor = System.Drawing.SystemColors.ActiveCaptionText
            Me.txtAgentName.Focus()
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter
        Try
            Dim Frm_Filter As New Frm_Filter
            Me.Cursor = Cursors.WaitCursor
            Frm_Filter.LoadHeaderCaption(Fields, "Frm_CSAgentInformation")
            Frm_Filter.ShowDialog()
            Me.Cursor = Cursors.Default
            ReBindGridCaption(Me.DGV_Master, "Frm_CSAgentInformation")
        Catch ex As Exception
            MsgBox("ActionFilter:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

        If Me.Validated1() = False Then Return False

        Dim bSuccess As Boolean = SaveData()

        Return bSuccess
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region

    Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click

    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

    End Sub

    Private Sub addColtrolEvents()

        For Each col As Control In Me.GroupBox1.Controls
            If TypeOf col Is TextBox OrElse TypeOf col Is ComboBox Then
                AddHandler col.KeyDown, AddressOf col_KeyDown
            End If
        Next
    End Sub

    Private Sub col_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            Me.btnFind.PerformClick()
        End If
    End Sub


End Class
